package com.anmong.parser.controller;

import cn.hutool.json.JSONObject;
import com.anmong.parser.dto.JsonDTO;
import com.anmong.parser.service.JsonParserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class ParserController {

    @Autowired
    private JsonParserService jsonParserService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @PostMapping("json")
    public JSONObject jsonObject(@RequestBody JsonDTO arrayStr){
        JSONObject vo = new JSONObject();
        try {
            String data = jsonParserService.parseJson(arrayStr.getArrayStr());
            vo.putOpt("data",data);
            vo.putOpt("code",200);
            logger.info(data);
        }
        catch (Exception e){
            logger.error("parse json error:{}",e.getMessage(),e);
            vo.putOpt("code",500);
            vo.putOpt("msg",e.getMessage());
            vo.putOpt("data",null);
        }
        return vo;
    }
}
